package com.example.exam001.service.impl.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.exam001.domain.Enterprise;
import com.example.exam001.mapper.EnterpiseMapper;
import com.example.exam001.service.impl.EnterpriseService;
import com.example.exam001.utils.ConverTo;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class EnterpriseServiceImpl extends ServiceImpl<EnterpiseMapper, Enterprise> implements EnterpriseService {
    @Override
    public Page<Enterprise> selectPage(Map<String, String> conditions) {
        // 构造一个条件查询对象
        QueryWrapper<Enterprise> queryWrapper = new QueryWrapper<>();
        // 构造一个分页查询对象
        int current = 1;
        if (conditions.containsKey("current")) {
            current = Integer.parseInt(conditions.get("current"));
        }
        int size = 10;
        if (conditions.containsKey("size")) {
            size = Integer.parseInt(conditions.get("size"));
        }
        Page<Enterprise> page = new Page<>(current, size);
        conditions.remove("current");
        conditions.remove("size");
        // 只有传入了条件查询参数才把条件加入到查询对象中
        if (!conditions.isEmpty()) {
            conditions.forEach((key, value) -> {
                queryWrapper.like(ConverTo.humpToUnderline(key), value);
            });
        }
        // 执行分页查询
        return this.getBaseMapper().selectPage(page, queryWrapper);
    }
}
